//
//  HDQRCodeInputVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/10.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDQRCodeInputVC.h"
#import "HDMapVC.h"

@interface HDQRCodeInputVC ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, strong) NSString *museumID;

@end

@implementation HDQRCodeInputVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKeyBoard)];
    [self.view addGestureRecognizer:myTap];
    _textField.text = @"T666999";
    [self pushFromQRCode:_textField.text];

}

- (void)resignKeyBoard
{
    [self.view endEditing:YES]; // view的所有子view，执行resignFirstResponder方法
}

- (IBAction)uploadNumberAction:(id)sender {
    [self resignKeyBoard];
    [self pushFromQRCode:_textField.text];
}

- (void) pushFromQRCode:(NSString *)number {
    if (_textField.text.length == 0)
        return;
    
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Ticket&a=scan_ticket", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"ticket_id": number,
                                 @"user_login": [[HDDeclare sharedDeclare] userPhoneID],
                                 @"museum_id": [[HDDeclare sharedDeclare] nowMuseumID]};
    
    [loginObj requestPath:urlPath Parameter:parameters withVC:self timeout:5.0f];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSNumber *statusCode = dataDic[@"status"];
        NSInteger statusInt = statusCode.integerValue;
        if (statusInt == 1) {
            NSDictionary *infoDic = dataDic[@"data"];
            _museumID = [NSString stringWithFormat:@"%@",infoDic[@"museum_id"]];
            
            NSString *nowMuseumID = [[HDDeclare sharedDeclare] nowMuseumID];
            if (_museumID.integerValue > 0 && [_museumID isEqualToString:nowMuseumID])
                [self performSegueWithIdentifier:@"QRCodeInputVCPushToMapLine" sender:nil];
            else if (_museumID.integerValue == 0) {
                NSDictionary *tipDic = dataDic[@"data"];
                [LCCoolHUD showFailure:tipDic[@"tips"] zoom:NO shadow:YES];
            }
            else if (_museumID.integerValue > 0) {
                NSDictionary *tipDic = dataDic[@"data"];
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:tipDic[@"tips"] preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self performSegueWithIdentifier:@"QRCodeInputVCPushToMapLine" sender:nil];
                }];
                [alertVC addAction:cancelAction];
                [alertVC addAction:okAction];
                [self presentViewController:alertVC animated:YES completion:NULL];
            }
            
            else {
                [LCCoolHUD showFailure:dataDic[@"msg"] zoom:NO shadow:YES];
            }
        }
        else {
            [LCCoolHUD showFailure:dataDic[@"msg"] zoom:NO shadow:YES];
        }
    };
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"QRCodeInputVCPushToMapLine"]) {
        HDMapVC *VC = segue.destinationViewController;
        VC.museumID = _museumID;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backAction:(id)sender {
    [super back];
}


@end
